﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace ChessAnotator.UserControls
{
    /// <summary>
    /// Interaction logic for Square.xaml
    /// </summary>
    public partial class Square : UserControl, INotifyPropertyChanged
    {
        #region Private Members
        private Pieces.BasePiece m_oPiece;
        #endregion

        #region Properties
        public Brush SquareBackground
        {
            get { return (Brush)GetValue(SquareBackgroundProperty); }
            set { SetValue(SquareBackgroundProperty, value); }
        }

        public static readonly DependencyProperty SquareBackgroundProperty =
            DependencyProperty.Register(
                "SquareBackground"
                , typeof(Brush)
                , typeof(Square)
                );

        public double OverlayOpacity
        {
            get { return (double)GetValue(OverlayOpacityProperty); }
            set { SetValue(OverlayOpacityProperty, value); }
        }
        public static readonly DependencyProperty OverlayOpacityProperty =
            DependencyProperty.Register(
                "OverlayOpacity"
                , typeof(double)
                , typeof(Square) 
                );

        public int Size
        {
            get { return (int)GetValue(SizeProperty); }
            set { SetValue(SizeProperty, value); }
        }
        public static readonly DependencyProperty SizeProperty =
            DependencyProperty.Register(
                "Size"
                , typeof(int)
                , typeof(Square)
                , new FrameworkPropertyMetadata(OnSizePropertyChanged)
                );
        private static void OnSizePropertyChanged(
            DependencyObject source
            , DependencyPropertyChangedEventArgs e
            )
        {
            Square oControl = source as Square;

            oControl.OnNotifyPropertyChanged("WhiteOverlaySize");
            oControl.OnNotifyPropertyChanged("BlackOverlaySize");
            oControl.OnNotifyPropertyChanged("WhiteOverlayLocation");
            oControl.OnNotifyPropertyChanged("WhiteNoteLocation");
            oControl.OnNotifyPropertyChanged("MoveMarkerSize");
        }

        private int m_nBlackAttackersCount = 0;
        public int BlackAttackersCount
        {
            get { return m_nBlackAttackersCount; }
            set 
            {
                m_nBlackAttackersCount = value;

                OnNotifyPropertyChanged("WhiteOverlaySize");
                OnNotifyPropertyChanged("BlackOverlaySize");
                OnNotifyPropertyChanged("WhiteOverlayLocation");
                OnNotifyPropertyChanged("BlackNoteText");
            }
        }

        private int m_nWhiteAttackersCount = 0;
        public int WhiteAttackersCount
        {
            get { return m_nWhiteAttackersCount; }
            set
            {
                m_nWhiteAttackersCount = value;

                OnNotifyPropertyChanged("WhiteOverlaySize");
                OnNotifyPropertyChanged("BlackOverlaySize");
                OnNotifyPropertyChanged("WhiteOverlayLocation");
                OnNotifyPropertyChanged("WhiteNoteText");
            }
        }
       
        public double WhiteOverlaySize
        {
            get
            {
                return (this.WhiteAttackersCount + this.BlackAttackersCount == 0) ? 0 : (this.Size * this.WhiteAttackersCount * 1.0) / (this.WhiteAttackersCount + this.BlackAttackersCount); 
            }
        }
        public double WhiteOverlayLocation
        {
            get
            {
                return this.Size - this.WhiteOverlaySize;
            }
        }

        public double BlackOverlaySize
        {
            get
            {
                return (this.WhiteAttackersCount + this.BlackAttackersCount == 0) ? 0 : (this.Size * this.BlackAttackersCount * 1.0) / (this.WhiteAttackersCount + this.BlackAttackersCount);
            }
        }

        public double WhiteNoteLocation
        {
            get 
            {
                return this.Size - 20;
            }
        }

        public string BlackNoteText
        {
            get
            {
                return (this.BlackAttackersCount > 0) ? this.BlackAttackersCount.ToString() : "";
            }
        }
        public string WhiteNoteText
        {
            get
            {
                return (this.WhiteAttackersCount > 0) ? this.WhiteAttackersCount.ToString() : "";
            }
        }

        public int MoveMarkerSize
        {
            get
            {
                return this.Size - 1;
            }
        }

        public bool IsOccupied
        {
            get
            { 
                return (ctlPiece.Content != null);
            }
        }

        public Pieces.BasePiece Piece
        {
            get
            {
                return m_oPiece;
            }
            set
            {
                m_oPiece = value;
            }
        }

        public int SquareNbr
        {
            get
            {
                return Base.Business.Utilities.GetSquareNbr(this.Name);
            }
        }
        #endregion

        public delegate void SquareSelectedEventHandler(object sender, EventArgs e);
        public event SquareSelectedEventHandler SquareSelected;

        public Square()
        {
            InitializeComponent();

            this.DataContext = this;
            this.OverlayOpacity = 1;
        }

        public void SetPiece(Control piece)
        {
            ctlPiece.Content = piece;
        }
        public void SetPiece(Pieces.BasePiece oPiece)
        {
            m_oPiece = oPiece;
            ctlPiece.Content = m_oPiece.PieceUI;
            oPiece.Square = this;
        }

        public void ClearPiece()
        {
            ctlPiece.Content = null;
            m_oPiece = null;
        }

        public bool HasPiece()
        {
            return (m_oPiece != null);
        }

        public void ShowMoveMarker()
        {
            this.MoveMarker.Visibility = Visibility.Visible;
        }
        public void HideMoveMarker()
        {
            this.MoveMarker.Visibility = Visibility.Collapsed;
        }

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnNotifyPropertyChanged(String propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        private void SquareHolder_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (this.IsOccupied)
            {
                this.MoveMarker.Visibility = Visibility.Visible;
            }

            if (SquareSelected != null)
            {
                SquareSelected(this, null);
            }
        }
    }
}
